package com.loongtech.bi.action.biplayer;

import com.loongtech.bi.action.BaseBiSearchAction;
import com.loongtech.bi.entity.bigr.ActiveSituation;
import com.loongtech.bi.entity.bigr.BaseMessage;
import com.loongtech.bi.entity.bigr.GatherGeneralSituation;
import com.loongtech.bi.entity.bigr.PaySituation;
import com.loongtech.bi.manager.player.BigRPlayerManager;
import com.loongtech.bi.manager.system.BiPlatformManager;
import com.loongtech.core.util.Time;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 玩家分析-大R管理Action
 *
 * @author Marin
 * @date 2018年03月28日
 */
@Controller
@Scope("session")
@RequestMapping("/player/bigRPlayer")
public class BigRPlayerBiAction extends BaseBiSearchAction {
    private static final long serialVersionUID = 1L;

    private BigRPlayerManager.BigRData data;
    @Autowired
    BigRPlayerManager bigRPlayerManager;

    @RequestMapping(value = "search.do", method = {RequestMethod.GET, RequestMethod.POST})
    public void search(HttpServletRequest request, HttpServletResponse response, @RequestParam String dt, String callback) {
        Map<String, Object> result = new HashMap<>();
        try {
            Date date = Time.formatDate(dt, Time.TIMEPATTERN_SIMPLE);

            data = bigRPlayerManager.getDatas(date);

            if (data != null) {
                for (ActiveSituation d : data.getActiveSituations()) {
                    d.setChannel(BiPlatformManager.getChannelNameById(d.getChannel()));
                }

                List<BaseMessage> bms = new ArrayList<>();
                Map<String, List<BaseMessage>> tmpMap = new HashMap<>();
                for (BaseMessage d : data.getBaseMessages()) {
                    List<BaseMessage> tmpList = tmpMap.get(d.getUserid());
                    if (tmpList == null) {
                        tmpList = new ArrayList<>();
                        tmpMap.put(d.getUserid(), tmpList);
                    }
                    tmpList.add(d);
                }
                for (Map.Entry<String, List<BaseMessage>> entry : tmpMap.entrySet()) {
                    BaseMessage baseMessage = null;
                    for (BaseMessage bm : entry.getValue()) {
                        if (baseMessage == null) {
                            baseMessage = new BaseMessage();
                            BeanUtils.copyProperties(baseMessage, bm);
                        } else {
                            baseMessage.setDayqian(baseMessage.getDayqian() + bm.getDayqian());
                            if (bm.getCreateday().compareTo(baseMessage.getCreateday()) < 0) {
                                baseMessage.setCreateday(bm.getCreateday());
                                baseMessage.setChannel(bm.getChannel());
                            }
                        }
                    }
                    baseMessage.setChannel(BiPlatformManager.getChannelNameById(baseMessage.getChannel()));
                    bms.add(baseMessage);
                }
                data.setBaseMessages(bms);

                for (GatherGeneralSituation d : data.getGatherGeneralSituations()) {
                    d.setChannel(BiPlatformManager.getChannelNameById(d.getChannel()));
                }
                for (PaySituation d : data.getPaySituations()) {
                    d.setChannel(BiPlatformManager.getChannelNameById(d.getChannel()));
                }
            }

            result.put("res", "0");
            result.put("data", data);

            printToJson(response, result, callback);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("res", "1");
            result.put("msg", e.getMessage());
            printToJson(response, result, callback);
        }
    }

    public void search() {
    }

    @Override
    protected String csvContent() {
        return null;
    }

    @Override
    protected String csvFileName() {
        return "大R管理";
    }

    public BigRPlayerManager.BigRData getData() {
        return data;
    }

    public void setData(BigRPlayerManager.BigRData data) {
        this.data = data;
    }
}
